package api

import (
	"context"
	"fmt"
	"github.com/emicklei/go-restful/v3"
	"github.com/infraboard/mcube/http/response"

	"gitee.com/kevin186/keyauth/apps/token"
	tokenImpl "gitee.com/kevin186/keyauth/apps/token/impl"
	"gitee.com/kevin186/keyauth/apps/user"
)

func (h *handler) Login(r *restful.Request, w *restful.Response) {
	ctx := context.Background()
	tokenRequest := token.NewIssueTokenRequest()

	req := user.NewQueryUserRequestFromHTTP(r.Request)
	set, err := h.service.QueryUser(r.Request.Context(), req)
	if err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}
	res := user.NewCreateUserRequest()

	if err := r.ReadEntity(res); err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}
	for _, v := range set.Items {
		h.log.Debug("数据库查询到用户：" + v.Data.Username)
		h.log.Debug("请求的用户：" + res.Username)
		fmt.Println("================")
		switch v.Data.Username {
		case res.Username:
			tokenRequest.UserName = res.Username
			tokenRequest.Password = res.Password

			// 应该是验证token，而不是每次登录都颁发一次token
			//// 获取请求头中的Token
			//accessToken := utils.GetToken(r.Request)
			//// 验证Token
			//req := token.NewValidateTokenRequest(accessToken)
			//ins, err := h.tokenService.ValidateToken(r.Request.Context(), req)
			//if err != nil {
			//	response.Failed(w.ResponseWriter, err)
			//	return
			//}

			issueToken, err := tokenImpl.Svr.IssueToken(ctx, tokenRequest)
			if err != nil {
				response.Failed(w.ResponseWriter, err)
			}
			response.Success(w.ResponseWriter, issueToken)
			return
		}
	}
	response.Failed(w.ResponseWriter, fmt.Errorf("用户不存在"))
}

func (h *handler) CreateUser(r *restful.Request, w *restful.Response) {
	req := user.NewCreateUserRequest()

	if err := r.ReadEntity(req); err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}

	set, err := h.service.CreateUser(r.Request.Context(), req)
	if err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}

	response.Success(w.ResponseWriter, set)
}

func (h *handler) QueryUser(r *restful.Request, w *restful.Response) {
	req := user.NewQueryUserRequestFromHTTP(r.Request)
	set, err := h.service.QueryUser(r.Request.Context(), req)
	if err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}
	response.Success(w.ResponseWriter, set)
}

func (h *handler) DescribeUser(r *restful.Request, w *restful.Response) {
	req := user.NewDescribeUserRequestById(r.PathParameter("id"))
	ins, err := h.service.DescribeUser(r.Request.Context(), req)
	if err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}

	response.Success(w.ResponseWriter, ins)
}

func (h *handler) UpdateUser(r *restful.Request, w *restful.Response) {
	req := user.NewPutUserRequest(r.PathParameter("id"))

	if err := r.ReadEntity(req.Data); err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}

	set, err := h.service.UpdateUser(r.Request.Context(), req)
	if err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}
	response.Success(w.ResponseWriter, set)
}

func (h *handler) PatchUser(r *restful.Request, w *restful.Response) {
	req := user.NewPatchUserRequest(r.PathParameter("id"))

	if err := r.ReadEntity(req.Data); err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}

	set, err := h.service.UpdateUser(r.Request.Context(), req)
	if err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}
	response.Success(w.ResponseWriter, set)
}

func (h *handler) DeleteUser(r *restful.Request, w *restful.Response) {
	req := user.NewDeleteUserRequestWithID(r.PathParameter("id"))
	set, err := h.service.DeleteUser(r.Request.Context(), req)
	if err != nil {
		response.Failed(w.ResponseWriter, err)
		return
	}
	response.Success(w.ResponseWriter, set)
}
